package com.shens.blog.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.shens.blog.entity.IPAgentEntity;
import com.shens.blog.service.RequestCallBack;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class RequestUtil {
    private static List<String> murls;
    private static RequestCallBack mcallback;
    static Thread rThread;
    static List<IPAgentEntity> ipList = getIp("https://gitee.com/shensong10/shens-blog/blob/master/Ips.txt");

    public RequestUtil() {
    }

    public static void requestData(List<String> urls, RequestCallBack requestCallBack) {
        murls = urls;
        mcallback = requestCallBack;
        rThread = RequestUtil.RequesThread.getInstance(new Runnable() {
            public void run() {
                RequestUtil.reques();
            }
        });
        rThread.start();
    }

    private static void reques() {
        if (murls != null && murls.size() != 0) {
            Iterator var1 = murls.iterator();

            while (true) {
                String url;
                do {
                    do {
                        if (!var1.hasNext()) {
                            return;
                        }

                        url = (String)var1.next();
                    } while (url == null);
                } while (url.equals(""));

                int count = 0;

                for (int i = 0; i < 10000; ++i) {
                    IPAgentEntity myIpAgentEntity = (IPAgentEntity)ipList.get(
                        (int)(Math.random() * (double)ipList.size()));
                    System.setProperty("http.maxRedirects", "50");
                    System.getProperties().setProperty("proxySet", "true");
                    System.getProperties().setProperty("http.proxyHost", myIpAgentEntity.getAddress());
                    System.getProperties().setProperty("http.proxyPort", myIpAgentEntity.getPort());

                    try {
                        Document doc = Jsoup.connect(url).userAgent("Mozilla").cookie("auth", "token").timeout(3000)
                            .get();
                        if (doc != null) {
                            ++count;
                            if (mcallback != null) {
                                mcallback.requesCallBack(url + "--成功刷新次数: " + count);
                            }
                        }
                    } catch (IOException var6) {
                        if (mcallback != null) {
                            mcallback.requesCallBack(
                                myIpAgentEntity.getAddress() + ":" + myIpAgentEntity.getPort() + "报错");
                        }
                    }
                }
            }
        }
    }

    public static List<IPAgentEntity> getIp(String url) {
        ArrayList ipList = new ArrayList();

        try {
            Document doc = Jsoup.connect(url).userAgent("Mozilla").cookie("auth", "token").timeout(30000).get();
            Pattern pattern = Pattern.compile("\\d+\\.\\d+\\.\\d+\\.\\d+:(\\d)*");
            Matcher matcher = pattern.matcher(doc.toString());
            ArrayList ips = new ArrayList();

            while (matcher.find()) {
                ips.add(matcher.group());
            }

            Iterator var7 = ips.iterator();

            while (var7.hasNext()) {
                String ip = (String)var7.next();
                IPAgentEntity myIp = new IPAgentEntity();
                String[] temp = ip.split(":");
                myIp.setAddress(temp[0].trim());
                myIp.setPort(temp[1].trim());
                ipList.add(myIp);
            }
        } catch (IOException var10) {
            if (mcallback != null) {
                mcallback.requesCallBack("加载错误:" + var10.toString() + "\r\n加载  代理ip地址出错:\r\n"
                    + "请移步：https://github.com/zqHero/FreeIpAgent/blob/master/Ips.txt  检查是否更改");
            }
        }

        IPAgentEntity ip1 = new IPAgentEntity("217.24.252.150","8080");
        IPAgentEntity ip2 = new IPAgentEntity("187.104.113.30","10444");
        IPAgentEntity ip3 = new IPAgentEntity("60.169.19.66","9000");
        IPAgentEntity ip4 = new IPAgentEntity("114.215.103.121","8081");
        IPAgentEntity ip5 = new IPAgentEntity("121.17.18.149","3128");
        IPAgentEntity ip6 = new IPAgentEntity("179.126.141.72","8080");


        IPAgentEntity ip7 = new IPAgentEntity("27.123.7.110","8080");
        IPAgentEntity ip8 = new IPAgentEntity("180.247.71.173","8080");
        IPAgentEntity ip9 = new IPAgentEntity("101.109.99.10","8080");
        IPAgentEntity ip10 = new IPAgentEntity("179.126.141.72","8080");
        IPAgentEntity ip11 = new IPAgentEntity("103.227.61.42","8080");
        IPAgentEntity ip12 = new IPAgentEntity("120.198.224.110","8080");
        IPAgentEntity ip13 = new IPAgentEntity("182.253.177.110","3128");
        IPAgentEntity ip14 = new IPAgentEntity("201.184.139.243","3128");

        IPAgentEntity ip15 = new IPAgentEntity("112.5.175.234","3128");
        IPAgentEntity ip16 = new IPAgentEntity("45.127.59.10","8080");
        IPAgentEntity ip17 = new IPAgentEntity("185.107.143.99","8080");
        IPAgentEntity ip18 = new IPAgentEntity("219.79.10.191","8080");
        IPAgentEntity ip19 = new IPAgentEntity("79.106.100.167","8080");
        IPAgentEntity ip20 = new IPAgentEntity("83.212.104.191","3128");
        IPAgentEntity ip21 = new IPAgentEntity("176.9.119.170","3128");






        /*
        86.63.9.126:8080
        121.196.226.246:84
        185.68.195.103:8080
        97.73.2.2:87
        52.44.43.132:80
        45.115.99.226:53281
        222.73.68.144:8090
        101.200.45.131:3128
        45.77.54.146:3128*/

        ipList.add(ip1);
        ipList.add(ip2);
        ipList.add(ip3);
        ipList.add(ip4);
        ipList.add(ip5);
        ipList.add(ip6);
        ipList.add(ip7);
        ipList.add(ip8);
        ipList.add(ip9);
        ipList.add(ip10);
        ipList.add(ip11);
        ipList.add(ip12);
        ipList.add(ip13);
        ipList.add(ip14);
        ipList.add(ip15);
        ipList.add(ip16);
        ipList.add(ip17);
        ipList.add(ip18);
        ipList.add(ip19);
        ipList.add(ip20);
        ipList.add(ip21);

        return ipList;
    }

    public static void exitCurrentThread() {
        if (rThread != null) {
            rThread.stop();
        }

    }

    static class RequesThread extends Thread {
        static RequestUtil.RequesThread thread;

        private RequesThread(Runnable runnable) {
            super(runnable);
        }

        public static synchronized RequestUtil.RequesThread getInstance(Runnable runnable) {
            if (thread != null) {
                thread.stop();
            }

            thread = new RequestUtil.RequesThread(runnable);
            return thread;
        }
    }
}
